﻿using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Furion.FriendlyException;
using System.Collections.Generic;

namespace Admin.NET.Application;
/// <summary>
/// 360服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class car_360Service : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<car_360> _rep;
    public car_360Service(SqlSugarRepository<car_360> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 分页查询360
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<car_360Output>> Page(car_360Input input)
    {
        var query = _rep.AsQueryable().Where(m => m.IsDelete == false)
             .LeftJoin<car_car>((m, pca) => m.carSubId == pca.Id)
                .LeftJoin<car_tag>((m, pca, pcol) => m.xilieId == pcol.Id)
                   .LeftJoin<car_tag>((m, pca, pcol,pcol2) => m.colorId == pcol2.Id)
                    .WhereIF(input.carId > 0, m => m.carId == input.carId)
                    .WhereIF(input.carSubId > 0, m => m.carSubId == input.carSubId)
                    .WhereIF(input.xilieId > 0, m => m.xilieId == input.xilieId)
                    .WhereIF(!string.IsNullOrWhiteSpace(input.imageUrl), m => m.imageUrl.Contains(input.imageUrl.Trim()))
                    .WhereIF(input.clickCount > 0, m => m.clickCount == input.clickCount)
                    .WhereIF(!string.IsNullOrWhiteSpace(input.colorName), m => m.colorName.Contains(input.colorName.Trim()))
                    .WhereIF(!string.IsNullOrWhiteSpace(input.xilieName), m => m.xilieName.Contains(input.xilieName.Trim()))
                    .WhereIF(input.colorId > 0, m => m.colorId == input.colorId)

                          .Select((m, pca,pcol,pcol2) => new car_360Output
                          {
                               Id=m.Id,
                              carId= (long)m.carId,
                               colorId=m.colorId,
                              imageUrl=m.imageUrl,
                                 carSubId =m.carSubId,
                                  xilieId=m.xilieId,
                                   colorName=pcol2.title,
                                    xilieName=pcol.title,
                              carName = pca.carname,
                            
                          });


                    


                   
;
        query = query.OrderBuilder(input);
        return await query.ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加360
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task Add(Addcar_360Input input)
    {
        var entity = input.Adapt<car_360>();
        await _rep.InsertAsync(entity);
    }

    /// <summary>
    /// 删除360
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(Deletecar_360Input input)
    {
        var entity = await _rep.GetFirstAsync(m => m.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _rep.FakeDeleteAsync(entity);   //假删除
    }

    /// <summary>
    /// 更新360
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(Updatecar_360Input input)
    {
        var entity = input.Adapt<car_360>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取360
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<car_360> Get([FromQuery] QueryByIdcar_360Input input)
    {
        return await _rep.GetFirstAsync(m => m.Id == input.Id);
    }

    /// <summary>
    /// 获取360列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<car_360Output>> List([FromQuery] car_360Input input)
    {
        return await _rep.AsQueryable().Select<car_360Output>().ToListAsync();
    }





}

